import time

from flask import Flask, Response
from flask import render_template

app = Flask(__name__)
# 定义主页路由
@app.route('/')
def home():
    return render_template('index.html')

# 定义 SSE 事件流
@app.route('/sse')
def sse_stream():
    def event_stream():
        for i in range(20):
            yield f"data: 测试数据20-Message {i}\n\n"
            time.sleep(1)

    return Response(event_stream(),
                    content_type='text/event-stream')

# 启动并指定端口
if __name__ == '__main__':
    app.run(port=8080, debug=True)
